package com.itlong.cloud.miniprogram.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.smartcard.SmartCardQRCodeContentDTO;
import com.itlong.cloud.POJO.VO.property.VisitorQrcodeVO;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.miniprogram.service.IMiniProgramPushMessageService;
import com.itlong.cloud.push.message.GatewayService;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.utils.date.DateUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <desc>
 *      小程序推送消息服务层接口
 * </desc>
 *
 * @createDate 2021/5/31
 **/
@Service
public class MiniProgramPushMessageServiceImpl implements IMiniProgramPushMessageService {



    @Autowired
    RedisService redisService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * <desc>
     *      推送小程序消息
     * </desc>
     *
     * @param visitorId 访客id
     * @return
     * @Author Zhu.yj
     * @createDate  2021/5/31
     */
    @Override
    public void pushMessage(String visitorId) throws Exception {


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("visitorId", visitorId);

        SmartCardQRCodeContentDTO smartCardQRCodeContentDTO = (SmartCardQRCodeContentDTO) redisService.getObject(RedisConstant.SMARTCARD_VISITOR_QR_CODE_CONTENT_CACHE_KEY + visitorId, SmartCardQRCodeContentDTO.class);

        //当该访客过期后不再推送变更二维码的消息给小程序
        if (smartCardQRCodeContentDTO == null){
            return;
        }

        String batchNumber = Integer.toHexString(Integer.parseInt(smartCardQRCodeContentDTO.getBatchNumber(), 16) + 1);
        String updateTime = DateUtil.fomatDate(new Date(), DateUtil.FULLYYYYMMDDHHMMSS);
        smartCardQRCodeContentDTO.setUpdateTime(updateTime);
        smartCardQRCodeContentDTO.setBatchNumber(batchNumber);
        redisService.set(RedisConstant.SMARTCARD_VISITOR_QR_CODE_CONTENT_CACHE_KEY + visitorId, RedisConstant.SMARTCARD_VISITOR_QR_CODE_CONTENT_CACHE_TIME, smartCardQRCodeContentDTO);

        String qrCodeContent = StringHandlerUtil.createQrCodeContent(smartCardQRCodeContentDTO.getEntrances() ,
                smartCardQRCodeContentDTO.getElevators(), smartCardQRCodeContentDTO.getProjectId(),
                smartCardQRCodeContentDTO.getStartDate(),smartCardQRCodeContentDTO.getValidTime(),batchNumber, smartCardQRCodeContentDTO.getProjectKey(),
                smartCardQRCodeContentDTO.getValidCount(),smartCardQRCodeContentDTO.getEntranceIdentifier(), smartCardQRCodeContentDTO.getUserId());
        jsonObject.put("content", qrCodeContent);

        //websocket服务端配了集群，使用redis发布/订阅模式解决信息不同步的问题
        stringRedisTemplate.convertAndSend(RedisConstant.WEBSOCKET_REDIS_MQ_TOPIC, jsonObject.toJSONString());
    }

    /**
     * <desc>
     *      将线下平台传入访客信息存入缓存
     * </desc>
     *
     * @param smartCardQRCodeContentDTO 访客二维码信息
     * @return
     * @Author Zhu.yj
     * @createDate  2021/6/3
     */
    @Override
    public void cacheVisitor(SmartCardQRCodeContentDTO smartCardQRCodeContentDTO) throws Exception {
        //设置uid
        //首次添加，将批次号设置为1
        smartCardQRCodeContentDTO.setBatchNumber("0001");
        String updateTime = DateUtil.fomatDate(new Date(), DateUtil.FULLYYYYMMDDHHMMSS);
        smartCardQRCodeContentDTO.setUpdateTime(updateTime);
        Date startDate = DateUtil.formatLong(Long.parseLong(smartCardQRCodeContentDTO.getStartDate()));
        SimpleDateFormat dateFormat = new SimpleDateFormat(DateUtil.YYMMDDHHMM);
        smartCardQRCodeContentDTO.setStartDate(dateFormat.format(startDate));
        redisService.set(RedisConstant.SMARTCARD_VISITOR_QR_CODE_CONTENT_CACHE_KEY + smartCardQRCodeContentDTO.getUserId(), RedisConstant.SMARTCARD_VISITOR_QR_CODE_CONTENT_CACHE_TIME, smartCardQRCodeContentDTO);
    }
}
